package routers

import (
	"fmt"
	"usplatformserver/global"
	"usplatformserver/internal/middleware"
	v1 "usplatformserver/internal/routers/api/v1"

	_ "usplatformserver/docs"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func NewRouter() *gin.Engine {

	gin.SetMode(global.ServerSettings.RunMode)
	r := gin.New()
	r.Use(gin.Logger())
	r.Use(middleware.Recovery())
	r.MaxMultipartMemory = 8 * 1024
	//r.Use(gin.Recovery())
	//r.Use(middleware.Translations()) //不能用，不支持高并发，并发会引发崩溃
	//r.Use(middleware.Cors())
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	r.Static("/img", fmt.Sprintf("%s/patientInfo", global.AppSettings.UsclientPath))

	patient := v1.NewPatient()

	apiDirect := r.Group("api/v1")
	{
		//登录验证
		apiDirect.POST("/login", v1.GetUser)
		//获取图片列表
		apiDirect.GET("/patient/images", patient.GetImageList)
		//获取病灶列表
		apiDirect.GET("/patient/lesions", patient.GetLesionsList)
		//获取图片组
		apiDirect.GET("/patient/image/group", patient.GetImageGroup)
		//根据图片信息获取病灶信息
		apiDirect.GET("/patient/image/lesions", patient.GetLesionsInfoByImageInfo)
		//根据时间戳倒序查询图片列表
		apiDirect.GET("/patient/images/time/desc", patient.GetImageListDescByTime)
		//根据时间戳倒序查询病灶列表
		apiDirect.GET("/patient/lesions/time/desc", patient.GetLesionsListDescByTime)
		//获取甲状腺评级标准
		gs := v1.NewGradingStandard()
		apiDirect.GET("/grading/standard", gs.GetGradingStandard)
		// 获取显示配置
		rv := v1.NewGetDisplayConfig()
		apiDirect.GET("/display/config", rv.GetDisplayConfig)
		//获取AI推荐结果
		ar := v1.NewAiRecommendReq()
		apiDirect.GET("/ai/recommend", ar.GetAiRecommendTubercles)
		//上传文件
		up := v1.NewUpload()
		apiDirect.POST("/upload/files", up.HandleUploadMultipleFile)
		apiDirect.POST("/upload/file", up.HandleUploadFile)
		apiDirect.POST("/upload/offline/images", up.HandleUploadOfflineImages)
		apiDirect.GET("/upload/files", up.List)
		apiDirect.GET("/upload/file/highlight", up.UploadHighlight) //根据文件ID获取所在页码

		//根据文件ID获取患者信息
		apiDirect.GET("/patient/fileid", patient.GetPatientInfoByFileId)
		//获取注册码
		apiDirect.GET("/licensecode", v1.GetLicenseCode)
		//获取版本号
		apiDirect.GET("/app/version", v1.GetVersion)
	}
	apiV1 := r.Group("api/v1")
	threshold := v1.NewThreshold()
	reports := v1.NewReportTemplate()
	product := v1.NewProductUsageRate()
	analysis := v1.NewStatisticsInfo()
	apiV1.Use(middleware.JWT()) //增加token验证
	{
		//登出
		apiV1.POST("/logout", v1.Logout)
		//患者列表
		apiV1.GET("/patients", patient.List)
		//获取检测阈值
		apiV1.GET("/threshold/det", threshold.GetThreshold)
		//设置检测阈值
		apiV1.POST("/threshold/det", threshold.SetThreshold)
		//获取报告模板
		apiV1.GET("/report/template", reports.GetReportTemplate)
		//设置报告模板
		apiV1.POST("/report/template", reports.SetReportTemplate)
		//获取LVEF阈值
		apiV1.GET("threshold/lvef", threshold.GetLVEFThreshold)
		//设置LVEF阈值
		apiV1.POST("threshold/lvef", threshold.SetLVEFThreshold)
		//导出数据
		apiV1.GET("/export", v1.Export)
		//下载文件
		apiV1.GET("download", v1.Download)
		//下载文件列表
		apiV1.GET("download/filelist", v1.GetDownloadFileList)
		//删除文件列表
		apiV1.DELETE("download/filelist", v1.DeleteDownloadFiles)
		//获取磁盘使用情况
		apiV1.GET("diskusage", v1.GetDiskUsage)
		//获取待下载文件总数
		apiV1.GET("download/downloadtimes", v1.GetDownloadTimes)
		// 获取产品使用率列表
		apiV1.GET("/product/usage", product.GetProductUsageRateData)
		// 使用率列表导出
		apiV1.GET("/product/usage/export", product.ExportProductUsageRateData)
		// 获取算法指标统计列表
		apiV1.GET("/analysis/statistics", analysis.GetStatisticsInfo)
		// 算法指标统计导出
		apiV1.GET("/analysis/statistics/export", analysis.ExportStatisticsInfo)
		// 删除导出文件
		apiV1.DELETE("/export", v1.DeleteExportFile)
		// 获取检查统计
		apiV1.GET("/statistics/checked", analysis.StatisticCheckedCount)
		// 导出检查量统计
		apiV1.POST("/statistics/checked/export", analysis.ExportStatisticCheckedCount)
		// 统计人工判读数据
		apiV1.GET("/artificial/checked/stat", v1.GetArtificialCheckedStc)
		// 统计心脏人工判读数据
		apiV1.GET("heart/artificial/checked/stat", v1.GetHeartArtificialStatistics)
		// 统计心脏人工判读数据第二版
		apiV1.GET("heart/artificial/checked/stat2", v1.GetHeartArtificialStatistics2)
		// 统计心脏算法统计数据
		apiV1.GET("analysis/statistics/checked/heart", analysis.GetHeartStatisticsInfo)
		// 快速复核详情数据
		apiV1.GET("/artificial/stat/detail", v1.GetArtificialStatisticsDetail)
	}

	return r
}
